﻿using RJCP.IO.Ports;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Threading;
using static GerberLibrary.Core.BOMEntry;
using System;

namespace CarPCBATest.Procedure
{
    internal class IV_3000T
    {
        private static object deviceDesc = "IV-3000T";

        [Conditional("DEVICE")]
        public static void Connect(string port, int baud = 9600)
        {
            SerialPortStream sp = new SerialPortStream(port, baud)
            {
                DataBits = 8,
                StopBits = StopBits.One,
                Parity = Parity.None,
            };
            sp.DataReceived += Sp_DataReceived;
            ManualResetEventSlim openCompletedEvent = new ManualResetEventSlim(false);
            Task.Run(() =>
            {
                sp.Open();
                openCompletedEvent.Set();
            });
            if (!openCompletedEvent.Wait(2000))
            {
                throw new Exception($"{deviceDesc}连接超时");
            }
            if (!sp.IsOpen)
            {
                throw new Exception($"{deviceDesc}打开串口失败");
            }
        }

        private static void Sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPortStream sp = (SerialPortStream)sender;
            sp.ReadLine();
        }

        [Conditional("DEVICE")]
        public static void Init()
        {
        }

        [Conditional("DEVICE")]
        public static void SetVoltage(int channel, int value)
        {
        }

        [Conditional("DEVICE")]
        public static void SetCurrent(int channel, int value)
        {
        }

        private static void SendCmd(string cmd)
        {
        }

        private static void SendCmdAndWaitReceived(string cmd)
        {
        }

        internal static void SwitchRemote()
        {
            throw new NotImplementedException();
        }
    }
}